#project(vic-context2)
#include(VicCopyVersion)
#copy_version(${PROJECT_NAME} GLOBAL)
#message(STATUS "${PROJECT_NAME} version: ${${PROJECT_NAME}_VERSION}")
message(STATUS "${PROJECT_NAME} binary dir: ${${PROJECT_NAME}_BINARY_DIR}")
message(STATUS "${PROJECT_NAME} source dir: ${${PROJECT_NAME}_SOURCE_DIR}")
message(STATUS "Project binary dir: ${PROJECT_BINARY_DIR}")
message(STATUS "Project source dir: ${PROJECT_SOURCE_DIR}")

#Find all source files in a directory.
aux_source_directory(./ ${PROJECT_NAME}_src_list)
message(STATUS ${${PROJECT_NAME}_src_list})

#Asm
if(VIC_ARCHITECTURE STREQUAL i386)
    set(ASM_ARCHITECTURE i386)
elseif(VIC_ARCHITECTURE STREQUAL amd64)
    set(ASM_ARCHITECTURE x86_64)
elseif(VIC_ARCHITECTURE STREQUAL armel OR VIC_ARCHITECTURE STREQUAL armhf)
    set(ASM_ARCHITECTURE arm)
elseif(VIC_ARCHITECTURE STREQUAL arm64)
    set(ASM_ARCHITECTURE arm64)
elseif(VIC_ARCHITECTURE STREQUAL mips)
    set(ASM_ARCHITECTURE mips32)
elseif(VIC_ARCHITECTURE STREQUAL powerpc)
    set(ASM_ARCHITECTURE ppc32)
elseif(VIC_ARCHITECTURE STREQUAL ppc64el)
    set(ASM_ARCHITECTURE ppc64)
elseif(VIC_ARCHITECTURE STREQUAL sparc)
    if(VIC_ADDRESS_MODEL EQUAL 32)
        set(ASM_ARCHITECTURE sparc)
    else()
        set(ASM_ARCHITECTURE sparc64)
    endif()
endif()
if(NOT ASM_ARCHITECTURE)
    message(FATAL_ERROR "Fail to detect ASM_ARCHITECTURE.")
endif()

set(ASM_ABI ${VIC_ABI})
if(NOT ASM_ABI)
    message(FATAL_ERROR "Fail to detect ASM_ABI.")
endif()

if(VIC_BINARY_FORMAT STREQUAL mach-o)
    set(ASM_BINARY_FORMAT macho)
elseif(VIC_BINARY_FORMAT)
    set(ASM_BINARY_FORMAT ${VIC_BINARY_FORMAT})
endif()
if(NOT ASM_BINARY_FORMAT)
    message(FATAL_ERROR "Fail to detect ASM_BINARY_FORMAT.")
endif()

set(ASM_SUFFIX "gas.S")
if(ASM_ABI STREQUAL aapcs)
    if(VIC_BINARY_FORMAT STREQUAL pe)
        set(ASM_SUFFIX "armasm.asm")
    endif()
elseif(ASM_ABI STREQUAL ms)
    if(CMAKE_C_COMPILER_ID STREQUAL GNU)
        set(ASM_SUFFIX "gas.asm")
    else()
        set(ASM_SUFFIX "masm.asm")
    endif()
endif()

set(${PROJECT_NAME}_MAKE_CONTEXT_NAME "context/asm/make_${ASM_ARCHITECTURE}_${ASM_ABI}_${ASM_BINARY_FORMAT}_${ASM_SUFFIX}")
set(${PROJECT_NAME}_SWAP_CONTEXT_NAME "context/asm/jump_${ASM_ARCHITECTURE}_${ASM_ABI}_${ASM_BINARY_FORMAT}_${ASM_SUFFIX}")
message(STATUS "${PROJECT_NAME}_MAKE_CONTEXT_NAME: ${${PROJECT_NAME}_MAKE_CONTEXT_NAME}")
message(STATUS "${PROJECT_NAME}_SWAP_CONTEXT_NAME: ${${PROJECT_NAME}_SWAP_CONTEXT_NAME}")
list(APPEND ${PROJECT_NAME}_src_list ${${PROJECT_NAME}_MAKE_CONTEXT_NAME})
list(APPEND ${PROJECT_NAME}_src_list ${${PROJECT_NAME}_SWAP_CONTEXT_NAME})
list(APPEND ${PROJECT_NAME}_src_list "context/dummy.c") # fix compile error(Missing variable is: CMAKE_ASM_CREATE_SHARED_LIBRARY)
message(STATUS ${${PROJECT_NAME}_src_list})
#enable_language(ASM)

if(NOT ${PROJECT_NAME}_src_list)
    return()
endif()

#Static library
if(BUILD_STATIC_LIBRARY)
    message(STATUS "STATIC ${PROJECT_NAME}")
    add_library(${PROJECT_NAME}_a STATIC ${${PROJECT_NAME}_src_list})
    set_target_properties(${PROJECT_NAME}_a PROPERTIES OUTPUT_NAME "${PROJECT_NAME}")
    install(TARGETS ${PROJECT_NAME}_a DESTINATION lib)
endif()

#Shared library
if(BUILD_SHARED_LIBRARY)
    message(STATUS "SHARED ${PROJECT_NAME}")
    add_library(${PROJECT_NAME}_so SHARED ${${PROJECT_NAME}_src_list})
    set_target_properties(${PROJECT_NAME}_so PROPERTIES OUTPUT_NAME "${PROJECT_NAME}")
    set_target_properties(${PROJECT_NAME}_so PROPERTIES VERSION ${${PROJECT_NAME}_VERSION} SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR})
    install(TARGETS ${PROJECT_NAME}_so DESTINATION lib)
endif()

